<template>
  <div class="small-card">
    <div v-if="title" class="title">{{ title }}</div>
    <div class="content">
      <slot />
    </div>
    <div class="export">
      <div class="left">输出变量</div>
      <div v-show="!editName" class="middle">{{ name }}</div>
      <el-input
        v-show="editName"
        ref="variable-name-input"
        v-model="name"
        class="edit-title-input"
        style="width: 55%"
        @blur="variableNameChangeBlur"
      />
      <div class="right"><i class="el-icon-edit-outline" @click="editVariableName" /></div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'SmallCard',
    props: {
      title: {
        type: String,
        default: '',
      },
      varName: {
        type: String,
        default: '',
      },
    },
    data() {
      return {
        name: '',
        editName: false,
      }
    },
    mounted() {
      this.name = this.varName
    },
    methods: {
      editVariableName() {
        this.editName = true
        this.$nextTick(() => {
          this.$refs['variable-name-input'].focus()
        })
      },
      variableNameChangeBlur() {
        this.editName = false
        this.$emit('change-name', this.name)
      },
    },
  }
</script>

<style scoped lang="scss">
  .title {
    color: #333333;
    font-size: 16px;
    margin: 0 0 10px;
  }

  .export {
    display: flex;
    align-items: center;
    margin: 15px 0 0;

    .left {
      flex: 1;
      white-space: nowrap;
    }

    .middle {
      font-size: 12px;
      flex: 4;
      text-align: center;
    }

    .edit-title-input {
      ::v-deep .el-input__inner {
        height: 23px;
        line-height: 23px;
      }
    }

    .right {
      flex: 1;
      text-align: right;

      .el-icon-edit-outline {
        cursor: pointer;
      }
    }
  }
</style>
